//
//  T3FMapPlatformView+annotation.swift
//  t3_map
//
//  Created by Cunhai Li on 2024/10/31.
//

import Foundation
import Bundle_Log

/// annotation处理
extension T3FMapPlatformView {

    func toAnnotationOptions(from arguments: Any?) -> [T3FAnnotationOptions]? {
        do {
            let json = try T3FJsonUtil.toJson(arguments)
            if let annotations = json["annotations"] as? [[String: Any]] {
                var annotationOptionsList: [T3FAnnotationOptions] = []
                for annotation in annotations {
                    if let annotationOptions = T3FAnnotationOptions(from: annotation) {
                        annotationOptionsList.append(annotationOptions)
                    }
                    else {
                        T3Log.error("annotations初始化异常，参数错误，continue")
                    }
                }
                return annotationOptionsList
            }
            else {
                T3Log.error("Annotation初始化异常，未找到`annotations`字段或者annotations字段类型转换异常")
                return nil
            }
        } catch (let error) {
            T3Log.error("Annotation初始化异常，error = \(error.localizedDescription)")
            return nil
        }
    }

    func add(annotations arguments: Any?) {
        if let options = toAnnotationOptions(from: arguments) {
            mapView.add(annotations: options)
        }
        else {
            T3Log.error("地图标注添加异常：数据转换失败")
        }
    }

    func remove(annotations arguments: Any?) {
        if let options = toAnnotationOptions(from: arguments) {
            mapView.remove(annotations: options)
        }
        else {
            T3Log.error("地图标注移除异常：数据转换失败")
        }
    }

    /// 更新标注物
    func update(annotation arguments: Any?) {
        do {
            let json = try T3FJsonUtil.toJson(arguments)
            if let annotationOption = T3FAnnotationOptions(from: json) {
                mapView.update(annotation: annotationOption)
            } else {
                T3Log.error("annotations初始化异常，参数错误，continue")
            }
        } catch(let e) {
            T3Log.error("annotations初始化异常：\(e.localizedDescription)，continue")
        }
    }

    func show(annotations arguments: Any?) {
        do {
            let json = try T3FJsonUtil.toJson(arguments)
            if let options = toAnnotationOptions(from: arguments) {

                let animated = json["animated"] as? Bool ?? true

                var edgeInsets = UIEdgeInsets.zero
                if let edgeInsetsValue = json["edgeInsets"] as? [String: Double] {
                    edgeInsets = T3FJsonUtil.edgeInsetsFrom(json: edgeInsetsValue) ?? .zero
                }
                
                mapView.show(annotations: options, edgePadding: edgeInsets, animated: animated)
            }
            else {
                T3Log.error("地图标注添加异常：数据转换失败")
            }
        } catch (let error) {
            T3Log.error("annotations初始化异常，error = \(error.localizedDescription)")
        }
    }
}
